package com.ljx.recruitment.interview.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljx.recruitment.common.domain.vo.resp.PageResp;
import com.ljx.recruitment.common.enums.CommonStatusTypeEnum;
import com.ljx.recruitment.common.exception.ApiException;
import com.ljx.recruitment.interview.domain.entity.Job;
import com.ljx.recruitment.interview.domain.entity.JobType;
import com.ljx.recruitment.interview.domain.vo.req.JobPageReq;
import com.ljx.recruitment.interview.domain.vo.req.JobTypeSubmitReq;
import com.ljx.recruitment.interview.domain.vo.resp.JobTypeResp;
import com.ljx.recruitment.interview.mapper.JobTypeMapper;
import com.ljx.recruitment.interview.service.JobTypeService;
import com.ljx.recruitment.security.util.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-11-02  11:10
 */
@Service
public class JobTypeServiceImpl extends ServiceImpl<JobTypeMapper, JobType> implements JobTypeService {

    @Autowired
    private JobTypeMapper jobTypeMapper;

    @Override
    public JobTypeResp submitJobType(JobTypeSubmitReq req) {
        UserUtil.checkIsSameTenant();
        JobType jobType;
        if (Objects.isNull(req.getId())){
            checkSubmitParams(req);
            jobType = BeanUtil.copyProperties(req, JobType.class);
            fillSubmitParams(jobType);
            jobTypeMapper.insert(jobType);
        } else {
            jobType = jobTypeMapper.selectById(req.getId());
            BeanUtils.copyProperties(req, jobType);
            jobTypeMapper.updateById(jobType);
        }
        return getJobType(jobType.getId());
    }

    private void fillSubmitParams(JobType jobType) {
        jobType.setTenantId(UserUtil.tryToGetTenantId());
    }

    public JobTypeResp getJobType(Long jobTypeId){
        JobType jobType = jobTypeMapper.selectOne(new LambdaQueryWrapper<JobType>()
                .eq(JobType::getId, jobTypeId)
                .eq(JobType::getStatus, CommonStatusTypeEnum.NORMAL.getKey()));
        return BeanUtil.copyProperties(jobType, JobTypeResp.class);
    }

    private void checkSubmitParams(JobTypeSubmitReq req) {
        if (Objects.isNull(req.getName())){
            throw new ApiException("参数不能为空");
        }
    }

    @Override
    public PageResp<JobType> getJobTypePage(JobPageReq req) {
        LambdaQueryWrapper<JobType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(JobType::getTenantId, UserUtil.tryToGetTenantId());
        if (StrUtil.isNotEmpty(req.getKey())){
            lambdaQueryWrapper.like(JobType::getName, req.getKey());
        }
        if (UserUtil.checkIsTenantAdmin()){
            if (Objects.nonNull(req.getStatus())){
                lambdaQueryWrapper.eq(JobType::getStatus, req.getKey());
            }
        } else {
            lambdaQueryWrapper.eq(JobType::getStatus, CommonStatusTypeEnum.NORMAL.getKey());
        }

        Page<JobType> page = jobTypeMapper.selectPage(new Page<>(req.getCurrPage(), req.getPageSize()), lambdaQueryWrapper);

        return PageResp.<JobType>builder()
                .total(page.getTotal())
                .list(page.getRecords())
                .build();
    }

}
